package com.gfxy.modules.statistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.aspect.DictTranslateService;
import com.gfxy.modules.common.controller.TeacherTotalScoreExcelView;
import com.gfxy.modules.common.controller.TotalScoreYearExcelView;
import com.gfxy.modules.statistics.service.ICoreSummaryService;
import com.gfxy.modules.statistics.vo.CoreSummaryForOrgVo;
import com.gfxy.modules.statistics.vo.CoreSummaryForSchoolVo;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.service.ISysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

@Api(tags="总分")
@RestController
@RequestMapping("statistics/coreSummary")
@Slf4j
public class CoreSummaryController {

    @Autowired
    private ICoreSummaryService coreSummaryService;
    @Autowired
    private DictTranslateService dictTranslateService;

    @Autowired
    private ISysDepartService sysDepartService;

    @AutoLog(value = "通过时间和部门，查询每个教师的教学建设总分")
    @ApiOperation(value="通过时间和部门，查询每个教师的教学建设总分", notes="通过时间和部门，查询每个教师的教学建设总分")
    @GetMapping(value = "/listSummaryForSchool")
    public Result<?> listSummaryForSchool(@RequestParam(name="date",required=true) String date) {
        List<CoreSummaryForSchoolVo> coreSummaryForSchoolVoList = coreSummaryService.listSummaryForSchool(date);
        return Result.OK(coreSummaryForSchoolVoList);
    }

    @AutoLog(value = "通过时间和部门，查询每个教师的教学建设总分")
    @ApiOperation(value="通过时间和部门，查询每个教师的教学建设总分", notes="通过时间和部门，查询每个教师的教学建设总分")
    @GetMapping(value = "/listSummaryForOrg")
    public Result<?> listSummaryForOrg(@RequestParam(name="date",required=true) String date,
                                       @RequestParam(name="orgCode",required=true) String orgCode,
                                        @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        Page<CoreSummaryForOrgVo> coreSummaryForOrgVoList = coreSummaryService.listSummaryForOrg(date, orgCode,pageNo, pageSize);
        return Result.OK(coreSummaryForOrgVoList);
    }


    @RequestMapping(value = "/exportXlsTemplate")
    public ModelAndView exportXls(
            @RequestParam(name="orgCode") String orgCode,
            @RequestParam(name="date") String date,
            HttpServletRequest request)  {
        try{
            List<CoreSummaryForOrgVo> coreSummaryForOrgVoList =  coreSummaryService.listSummaryForOrg(date,orgCode);

            SysDepart depart = sysDepartService.getById(orgCode);
            String orgName = depart.getDepartName();

            Iterator var3 = coreSummaryForOrgVoList.iterator();
            List<JSONObject> items = dictTranslateService.translateDict(var3);


            CoreTotalScoreForOrgExcelView coreTotalScoreForOrgExcelView = SpringContextHolder.getBean(CoreTotalScoreForOrgExcelView.class);
            ModelAndView mv = new ModelAndView(coreTotalScoreForOrgExcelView);
            mv.addObject(CoreTotalScoreForOrgExcelView.TITLE, date + "年 " + orgName + " 发展关键核心指标积分汇总");
            mv.addObject(CoreTotalScoreForOrgExcelView.CONTENT, items);
            return mv;
        }catch (Exception e){
            throw e;
        }
    }

    @RequestMapping(value = "/exportXlsSchoolData")
    public ModelAndView exportXlsSchoolData(
            @RequestParam(name="date") String date,
            HttpServletRequest request)  {
        try{
            List<CoreSummaryForSchoolVo> coreSummaryForSchoolVoList = coreSummaryService.listSummaryForSchool(date);
            List<CoreSummaryForOrgVo> coreSummaryForOrgVoList = coreSummaryService.listSummary(date);



            Iterator var3 = coreSummaryForOrgVoList.iterator();
            List<JSONObject> items = dictTranslateService.translateDict(var3);




            CoreTotalScoreForSchoolExcelView  coreTotalScoreForOrgExcelView = SpringContextHolder.getBean(CoreTotalScoreForSchoolExcelView.class);
            ModelAndView mv = new ModelAndView(coreTotalScoreForOrgExcelView);
            mv.addObject(CoreTotalScoreForSchoolExcelView.TITLE, date + "年 发展关键核心指标积分汇总");
            mv.addObject(CoreTotalScoreForSchoolExcelView.CONTENT,coreSummaryForSchoolVoList);
            mv.addObject(CoreTotalScoreForSchoolExcelView.CONTENT_1, items);
            return mv;
        }catch (Exception e){
            throw e;
        }
    }
}
